<template>
  <div id="detail">
    <!-- 获取动态路由传递过来的id
    {{ $route.params.iid }}
    -->
    <detail-nav-bar @titleClick="titleClick" ref="nav"> </detail-nav-bar>
    <scroll
      :probeType="3"
      :pullUpLoad="false"
      ref="scroll"
      @scrollY="contentScroll"
    >
      <swiper
        :banners="topImages"
        class="detail-swiper"
        @swiperImgLoad="swiperImgLoad"
      ></swiper>
      <detail-base-info :goods="goods" :shop-name="shop.name"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info
        :detailInfo="detailInfo"
        @imageLoad="swiperImgLoad"
      ></detail-goods-info>
      <detail-param-info
        :paramInfo="paramInfo"
        ref="params"
      ></detail-param-info>
      <detail-comment-info
        :commentInfo="commentInfo"
        @imageLoad="swiperImgLoad"
        ref="comment"
      ></detail-comment-info>
      <goods-list
        :goods="recommends"
        @itemImgLoad="swiperImgLoad"
        ref="recommends"
      ></goods-list>
    </scroll>
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <back-top v-if="backTopGlog" @click.native="backTop"></back-top>

    <!-- <toast :message="message" :show="show"></toast> -->
  </div>
</template>
<script>
import { mapActions } from "vuex";
import bus from "utils/homeImgBus/bus";
import { getDetail, Goods, Shop, GoodsParam, getRecommend } from "api/detail";

import DetailNavBar from "./childComps/DetailNavBar";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import DetailBottomBar from "./childComps/DetailBottomBar";

import Swiper from "components/common/swiper/Swiper";
import Scroll from "components/common/scroll/Scroll";
import GoodsList from "components/content/goods/GoodsList";

import { debounce } from "@/common/utils";
import { itemListenerMixin, backTopMixin } from "@/common/mixin.js";

// import Toast from "components/common/toast/Toast";

export default {
  name: "Detail",
  components: {
    Swiper,
    Scroll,
    DetailNavBar,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
  },
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      navBarObj: {},
      themeTopYs: [],
      getThemeTopYs: null,
      currentIndex: 0,
    };
  },
  mounted() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid;
    // 2.根据iid请求详情数据
    getDetail(this.iid).then((res) => {
      const data = res.data.result;
      // 1.获取顶部轮播图数据
      this.topImages = data.itemInfo.topImages;
      // 2.获取商品信息
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      // 3.获取商家信息
      this.shop = new Shop(data.shopInfo);
      // 4.保存商品的详情数据
      this.detailInfo = data.detailInfo;
      // 5.获取参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      // 6.获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    });
    // 3.请求推荐信息
    getRecommend().then((res) => {
      this.recommends = res.data.data.list;
    });
    // 4.监听图片加载完成,重新计算一下scrollerHeight(这块是公共的，我们将这里抽离出来放到了mixin.js中)

    // 5.给getThemeTopYs赋值
    this.getThemeTopYs = debounce(() => {
      // this.$nextTick(() => {
      // 根据最新的数据，对应的DOM是已经被渲染出来
      // 但是图片依然是没有加载完；offsetTop值不对，一般都是因为图片加载问题
      // });
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(-this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(-this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(-this.$refs.recommends.$el.offsetTop);
      // console.log(this.themeTopYs);
    });
  },
  destroyed() {
    // 由于详情页没事使用keep-alive
    // 2.取消全局事件的监听
    bus.$off("itemImgLoad", this.itemImgListener);
  },
  methods: {
    ...mapActions(["addCart"]),
    // better-scroll会受到swiper图片加载高度的影响，需要swiper图片加载后重新计算一下滚动的高度
    swiperImgLoad() {
      this.$refs.scroll.refresh();
      this.getThemeTopYs();
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, this.themeTopYs[index], 500);
    },
    contentScroll(y) {
      // y和themeTopYs中的值进行对比
      // console.log(y);
      // i ===> String
      for (let i in this.themeTopYs) {
        if (
          (parseInt(i) < this.themeTopYs.length - 1 &&
            y >= -this.themeTopYs[parseInt(i)] &&
            y < -this.themeTopYs[parseInt(i) + 1]) ||
          (parseInt(i) === this.themeTopYs.length - 1 &&
            y >= -this.themeTopYs[parseInt(i)])
        ) {
          if (this.currentIndex !== parseInt(i)) {
            // console.log(i);
            this.currentIndex = parseInt(i);
            // 直接给子组件的data中currentIndex赋值
            this.$refs.nav.currentIndex = this.currentIndex;
          }
        }
      }
      if (y >= 1000) {
        this.backTopGlog = true;
      } else {
        this.backTopGlog = false;
      }
    },
    // 添加购物车
    addToCart() {
      // 1.获取购物车需要展示的信息
      const product = {};
      product.iid = this.iid;
      product.imgURL = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.newPrice = this.goods.newPrice;
      // 2.将商品添加到购物车里面(Store)
      this.addCart(product).then((res) => {
        this.$toast.show(res, 2000)
      });
    },
  },
};
</script>
<style lang="scss" scoped>
#detail {
  position: relative;
  height: 100vh;
}
.detail-swiper {
  height: 300px;
  overflow: hidden;
}
.wrapper {
  bottom: 0;
}
</style>
